package md.mru.core.service;

import md.mru.core.common.BaseDtoMapper;
import md.mru.core.domains.administration.Event;
import md.mru.core.dtos.*;
import md.mru.core.repositories.EventRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sergiu.marin
 * @since 16.04.2014.
 */
@Service
public class EventService {

    @Autowired
    @Qualifier(value = "dtoMapper")
    private BaseDtoMapper mapper;
    @Autowired
    private EventRepository eventRepository;

    public EventsWrapper loadNextEvents(long userId, long groupId, int first, int size) {
        PageRequest request = new PageRequest(first, size, Sort.Direction.DESC, "createdDate");
        Page<Event> events = eventRepository.loadNextEvents(userId, groupId, request);
        List<EventDto> eventDtos = mapper.mapList(events.getContent(), EventDto.class);
        return new EventsWrapper(eventDtos, events.getTotalElements());
        }

    public void createNewEvent(EventDto eventDto) {
        Event event = mapper.map(eventDto, Event.class);
        eventRepository.save(event);
    }
}
